package com.ruoyi.webs.utils.weixinlogin;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

public class MessageUtil {
    public static final String RESP_MESSAGE_TYPE_TEXT = "text";
    public static final String RESP_MESSAGE_TYPE_MUSIC = "music";
    public static final String RESP_MESSAGE_TYPE_NEWS = "news";
    public static final String REQ_MESSAGE_TYPE_TEXT = "text";
    public static final String REQ_MESSAGE_TYPE_IMAGE = "image";
    public static final String REQ_MESSAGE_TYPE_LINK = "link";
    public static final String REQ_MESSAGE_TYPE_LOCATION = "location";
    public static final String REQ_MESSAGE_TYPE_VOICE = "voice";
    public static final String REQ_MESSAGE_TYPE_EVENT = "event";
    public static final String EVENT_TYPE_SUBSCRIBE = "subscribe";
    public static final String EVENT_TYPE_UNSUBSCRIBE = "unsubscribe";
    public static final String EVENT_TYPE_CLICK = "CLICK";

    public static Map<String, String> parseXml(HttpServletRequest request) throws Exception {
        Map<String, String> map = new HashMap<>();


        ServletInputStream servletInputStream = request.getInputStream();

//        SAXReader reader = new SAXReader();
//        Document document = reader.read((InputStream) servletInputStream);
//
//        Element root = document.getRootElement();
//
//        List<Element> elementList = root.elements();
//
//
//        for (Element e : elementList) {
//            map.put(e.getName(), e.getText());
//        }

        servletInputStream.close();
        servletInputStream = null;

        return map;
    }


//    private static XStream xstream = new XStream((HierarchicalStreamDriver) new Object());


    public static String extract(String html) {
        StringBuffer buffer = null;

//        String dateTag = getMonthDay(0);
//
//        Pattern p = Pattern.compile("(.*)(<div class=\"listren\">)(.*?)(</div>)(.*)");
//        Matcher m = p.matcher(html);
//        if (m.matches()) {
//            buffer = new StringBuffer();
//            if (m.group(3).contains(getMonthDay(-1))) {
//                dateTag = getMonthDay(-1);
//            }
//
//            buffer.append("≡≡").append(" 历史上的  ").append(dateTag).append("≡≡").append("\n\n");
//            byte b;
//            int i;
//            String[] arrayOfString;
//            for (i = (arrayOfString = m.group(3).split("  ")).length, b = 0; b < i; ) {
//                String info = arrayOfString[b];
//                info = info.replace(dateTag, "").replace("（图）", "").replaceAll("</?[^>]+>", "").replace("&nbsp;", "").trim();
//
//                if (!"".equals(info)) {
//                    buffer.append(info).append("\n\n");
//                }
//                b++;
//            }
//
//        }
        return (buffer == null) ? null : buffer.substring(0, buffer.lastIndexOf("\n\n"));
    }


    public static String getMonthDay(int diff) {
        DateFormat df = new SimpleDateFormat("M月d日");
        Calendar c = Calendar.getInstance();
        c.add(6, diff);
        return df.format(c.getTime());
    }


    public static String getWeather(String cityName) {
        return "w";
    }
}